package com.fwtai.service.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.tool.ToolClient;
import com.fwtai.web.OrderLogDao;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

/**
 * 异步处理对账表业务层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2022-06-15 18:15:41
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class OrderLogService{

  private final OrderLogDao orderlogDao;

  private final ThreadPoolTaskExecutor taskExecutor;

  public OrderLogService(final OrderLogDao orderlogDao,final ThreadPoolTaskExecutor taskExecutor){
    this.orderlogDao = orderlogDao;
    this.taskExecutor = taskExecutor;
  }

  public String queryById(final Long kid){
    return CompletableFuture.supplyAsync(()->{
      if(kid == null){
        return ToolClient.jsonValidateField();
      }
      return ToolClient.queryJson(orderlogDao.queryById(kid));
    },taskExecutor).join();
  }

  public String listData(final PageFormData formData){
    try{
      final PageFormData params = ToolClient.dataTableMysql(formData);
      if(params == null)return ToolClient.jsonValidateField();
      return ToolClient.dataTableOK(CompletableFuture.supplyAsync(() -> orderlogDao.getListData(params),taskExecutor).join(),CompletableFuture.supplyAsync(() -> orderlogDao.getListTotal(params),taskExecutor).join());
    } catch (final Exception ignored){
      return ToolClient.dataTableException();
    }
  }
}